using System.Configuration;
using System.Linq;

namespace gpcx
{
    public class ConfigManager
    {
        public static string GetAppSettingsKeyValue(string keyName)
        {
            var keyvalue = ConfigurationManager.AppSettings.Get(keyName);
            if (keyvalue == null)
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings.Add(keyName, "");
                config.Save();
            }
            return keyvalue;
        }

        public static void UpdateSetting(string keyName, string newValue)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings.AllKeys.Contains(keyName))
            {
                config.AppSettings.Settings[keyName].Value = newValue;
            }
            else
            {
                config.AppSettings.Settings.Add(keyName, newValue);
            }
            config.Save();
            ConfigurationManager.RefreshSection("appSettings");
        }
    }
}
